package cn.jjzd.info.service;

import cn.jjzd.info.mapper.BusinessTypeMapper;
import cn.jjzd.info.pojo.BusinessType;
import cn.jjzd.info.response.SelectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusinessTypeService {
    @Autowired
    private BusinessTypeMapper businessTypeMapper;

    /**
     * 查询所有业务
     * @return
     */
    public List<SelectResponse> queryList() {
        //初始化example
        Example example = new Example(BusinessType.class);
        example.setOrderByClause(" id asc ");

        List<SelectResponse> responses = new ArrayList<>();
        SelectResponse response = new SelectResponse();
        response.setId(0L);
        response.setName("全部");
        responses.add(response);

        List<BusinessType> businessTypes = businessTypeMapper.selectByExample(example);
        List<SelectResponse> selectResponses = businessTypes.stream().map(businessType -> {
            SelectResponse selectResponse = new SelectResponse();
            selectResponse.setId(businessType.getId());
            selectResponse.setName(businessType.getBusinessName());
            return selectResponse;
        }).collect(Collectors.toList());
        responses.addAll(selectResponses);
        return responses;
    }

    public BusinessType queryById(Long id){
        return businessTypeMapper.selectByPrimaryKey(id);
    }

    public SelectResponse queryBusinessTypeId(Long id) {
        BusinessType businessType = this.queryById(id);
        SelectResponse response = null;
        if(null != businessType){
            response = new SelectResponse();
            response.setId(businessType.getId());
            response.setName(businessType.getBusinessName());
        }
        return response;
    }
}
